package com.apestech.api.workflow.modul;

import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * 加签的数据模型
 *
 * @author hup
 * @create 2018-07-10-18:35
 */
public class AddSignModul {

    private ArrayList<FlowUser> userlist=new ArrayList<FlowUser>();
    private int order;
    private String userId;
    private String userName;
    private String inUser="";

    public AddSignModul(){ }

    public AddSignModul(String text) throws Exception {
        iniClaimModul(JSON.toJSON(text));
    }

    public AddSignModul(JSONObject map) throws Exception {
        iniClaimModul(map);
    }

    private void iniClaimModul(JSONObject map) throws Exception {
        ArrayList ulist= (ArrayList) map.get("userlist");
        if(ulist!=null){
            int len=ulist.size();
            if(len>0){
                if(ulist.get(0) instanceof FlowUser){
                    this.userlist=(ArrayList<FlowUser>) ulist;
                } else {
                    for(int i=0; i<len; i++){
                        this.userlist.add(new FlowUser(new JSONObject((Map)ulist.get(i))));
                    }
                }
            }
        }
        this.userId=map.getString("userId");
        this.userName=map.getString("userName");
        this.order=map.getInteger("order");
        this.inUser=map.getString("inUser");
    }

    public AddSignModul(ArrayList<FlowUser> userlist, String userId, String userName, int order){
        this.userlist=userlist;
        this.userId=userId;
        this.userName=userName;
        this.order=order;
    }

    public ArrayList<FlowUser> getUserlist() {
        return userlist;
    }

    public void setUserlist(ArrayList<FlowUser> userlist) {
        this.userlist = userlist;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void add(String id, String name){
        if(this.userlist==null){
            this.userlist=new ArrayList<FlowUser>();
        }
        this.userlist.add(new FlowUser(id, name));
    }

    public void add(String id, String name, String candidateId, String candidateName){
        if(this.userlist==null){
            this.userlist=new ArrayList<FlowUser>();
        }
        this.userlist.add(new FlowUser(id, name, candidateId, candidateName));
    }

    public void set(int index, String id, String name){
        FlowUser flowUser=this.userlist.get(index);
        flowUser.set(id, name);
        this.userlist.set(index, flowUser);
    }

    public void set(int index, String id, String name, String candidateId, String candidateName){
        FlowUser flowUser=this.userlist.get(index);
        flowUser.set(id, name, candidateId, candidateName);
        this.userlist.set(index, flowUser);
    }

    public int size(){
        if(this.userlist==null) return -1;
        return this.userlist.size();
    }

    public String getInUser() {
        return inUser;
    }

    public void setInUser(String inUser) {
        this.inUser = inUser;
    }

    public void addInUser(String inUser){
        if(inUser==null) return;
        if(this.inUser==null) this.inUser="";
        if((","+this.inUser+",").indexOf(","+inUser+",")<0){
            if(!"".equals(this.inUser)){
                this.inUser=this.inUser+",";
            }
            this.inUser=this.inUser+inUser;
        }
    }

    public String getId(int index) {
        if(this.userlist==null) return null;
        if(this.userlist.size()==0) return userId;
        return this.userlist.get(index).getId();
    }

    public String getName(int index) {
        if(this.userlist==null ) return null;
        if(this.userlist.size()==0) return userName;
        return this.userlist.get(index).getName();
    }

    public String getCandidateId(int index) {
        if(this.userlist==null) return null;
        if(this.userlist.size()==0) return userId;
        return this.userlist.get(index).getCandidateId();
    }

    public String getCandidateName(int index) {
        if(this.userlist==null ) return null;
        if(this.userlist.size()==0) return userName;
        return this.userlist.get(index).getCandidateName();
    }

}
